<template>
	<view>
		<view class="subject">
			<view class="list-box" v-if="list.length > 0">
				<view class="list" v-for="(item, index) in list">
					<view class="list-card">
						<!-- <img class="list-img" src="@/static/gybg.png" alt=""> -->
						<view class="list-card-txt">
							<view v-if="type !=3">
								<text class="title-plan">{{ item.tpName }}{{item.paperIndex}}</text>
								<text class="start-time">{{formatDate(item.createTime)}}</text>
							</view>
							<view v-else>
								<text class="title-plan">{{ item.name }}用品</text>
								<text class="start-time">{{formatDate(item.createTime)}}</text>
							</view>
							<view v-if="type == 1" class="card-fs"
								:style="{color: item.sumTrue >= 90 ? '#2386FFFF': 'rgb(245 49 49 / 85%)'}">
								<text>{{item.sumTrue}}</text><text style="font-size: 36rpx;">分</text>
							</view>
							<view v-if="type == 2" class="card-fs"
								:style="{color: item.score >= 80 ? '#2386FFFF': 'rgb(245 49 49 / 85%)'}">
								<text>{{item.score}}</text><text style="font-size: 36rpx;">分</text>
							</view>
							<view v-if="type == 3" class="card-fs"
								:style="{color: item.score >= 10 ? '#2386FFFF': 'rgb(245 49 49 / 85%)'}">
								<text>{{item.score}}</text><text style="font-size: 36rpx;">件</text>
							</view>
						</view>
						<view class="subtitle-list">
							<view class="subtitle-list-card">
								<text class="card-title">用时</text>
								<text class="card-subtitle" >{{ timeDifference(item.startTime, item.createTime) }}</text>
							</view>
							<view class="subtitle-list-card">
								<text class="card-title">{{type !=3 ? '答错':'选错'}}</text>
								<text
									class="card-subtitle">{{type ==1 ? item.sumFalse : (type==2 ? item.errCount : item.errIds.length)  }}</text>
							</view>
							<view class="subtitle-list-card">
								<text class="card-title">{{type !=3 ? '答对':'选对'}}</text>
								<text
									class="card-subtitle">{{ type ==1 ?item.sumTrue : (type==2 ? item.fitCount : item.rightIds.length )}}</text>
							</view>
							<view class="subtitle-list-card">
								<text class="card-title">{{type !=3 ? '未答':'未选'}}</text>
								<text
									class="card-subtitle">{{type ==1 ?item.sumNone :(type==2 ? item.noneCount:item.noneIds.length)}}</text>
							</view>
						</view>
					</view>
					<view class="list-card-button">
						<view style="flex: 1;" @click="exercises(item)">错题练习</view>
						<template v-if="type != 3">
							<view class="line">
							</view>
							<view style="flex: 1;" @click="paperLook(item)">查看试卷</view>
						</template>
						
					</view>
				</view>
				<view v-if="isLoading && pageNum > 1" style="text-align:center;padding:20rpx;">加载中...</view>
				<view v-else-if="!hasMore && list.length > 0" style="text-align:center;padding:20rpx;">没有更多了</view>
			</view>
			<view class="none-data" v-else>
				<img class="none-img" src="@/static/nonedata.png" alt="">
				<view class="none-txt">
					<text style="display: block;font-size: 40rpx;color: #007aff;">暂无错题记录</text>
					<text style="padding: 20rpx;">请返回题库页面模拟考试</text>
				</view>
				<button @click="clickHome" class="none-btn">去模拟考试</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTestPaperList,
		getSubPaperList,
		getSub1PaperList
	} from '@/api/index.js'
	import {
		formatDate,
		timeDifference
	} from '@/utils/index'
	export default {
		data() {
			return {
				pageNum: 1,
				pageSize: 20,
				list: [],
				isLoading: false,
				hasMore: true,
				refreshing: false,
				formatDate,
				timeDifference,
				type: 1,
				userinfo: {},
				opc: null,
				id: null,
			};
		},
		onLoad(val) {
			this.userinfo = uni.getStorageSync('userinfo')
			this.type = val.type
			this.opc = val.opc
			this.id = val.id
			uni.setNavigationBarTitle({
				title: val.type == 1 ? '理论试卷列表' : (val.type == 2 ? '科三试卷列表' : '科目一试卷列表')
			})
			this.pageNum = 1;
			this.hasMore = true;
			this.loadData();
		},
		onShow() {
			// 保持刷新逻辑
		},
		onPullDownRefresh() {
			this.handlePullDownRefresh();
		},
		onReachBottom() {
			this.handleReachBottom();
		},
		methods: {
			clickHome() {
				uni.switchTab({
					url: this.type == 1 ? '/pages/index/index' : '/pages/subject/subject'
				})
			},
			handlePullDownRefresh() {
				this.pageNum = 1;
				this.hasMore = true;
				this.refreshing = true;
				this.loadData(() => {
					uni.stopPullDownRefresh();
					this.refreshing = false;
				});
			},
			handleReachBottom() {
				if (this.isLoading || !this.hasMore) return;
				this.pageNum++;
				this.loadData();
			},
			loadData(callback) {
				if (this.isLoading) return;
				this.isLoading = true;
				let params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
				};
				if (this.type == 2) {
					params.opc = this.opc;
					getSubPaperList(params).then(res => {
						this.handleList(res.data.list, callback);
					}).catch(() => {
						this.isLoading = false;
						if (callback) callback();
					});
				}
				if (this.type == 1) {
					uni.showLoading();
					params.otype = this.opc;
					getTestPaperList(params).then(res => {
						this.handleList(res.data.list, callback);
						uni.hideLoading();
					}).catch(() => {
						this.isLoading = false;
						uni.hideLoading();
						if (callback) callback();
					});
				}
				if (this.type == 3) {
					params.otype = this.opc;
					params.userId = this.userinfo.id;
					getSub1PaperList(params).then(res => {
						let newList = res.data.list.map(e => ({
							...e,
							errIds: JSON.parse(e.errIds),
							noneIds: JSON.parse(e.noneIds),
							rightIds: JSON.parse(e.rightIds),
							userIds: JSON.parse(e.userIds)
						}));
						this.handleList(newList, callback);
					}).catch(() => {
						this.isLoading = false;
						if (callback) callback();
					});
				}
			},
			handleList(newList, callback) {
				if (this.pageNum === 1) {
					this.list = newList;
				} else {
					this.list = this.list.concat(newList);
				}
				this.hasMore = newList.length === this.pageSize;
				this.isLoading = false;
				if (callback) callback();
			},
			// 错题练习 history_type  
			exercises(item) {
				console.log(item);
				if(item.errCount == 0){
					return uni.showToast({
						title:'无错题',
						icon:"none"
					})
				}
				if (this.type == 1) {
					uni.navigateTo({
						url: `/pages/exercises/exercises?id=${item.id}&type=1&opc=${item.otype}`
					})
				}
				if (this.type == 2) {
					uni.navigateTo({
						url: `/pages/operation/operation?id=${item.id}&model=1`
					})
				}
				if (this.type == 3){
					uni.navigateTo({
						url: `/pages/subject1/subject1?id=${item.id}&model=1`
					})
				}

			},
			// 试卷查看
			paperLook(item) {
				if (this.type == 1) {
					uni.navigateTo({
						url: `/pages/exercises/exercises?id=${item.id}&type=2&opc=${item.otype}`
					})
				}
				if (this.type == 2) {
					uni.navigateTo({
						url: `/pages/operation/operation?id=${item.id}&model=2`
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	page {
		box-shadow: 0px -10rpx 16rpx -6rpx #2069c7;
		z-index: 2;
		background: linear-gradient(180deg, #4d8dec, rgba(104, 157, 234, 0));
	}

	.subject {
		position: relative;
		top: -50rpx;
		border-radius: 50rpx 50rpx 0rpx 0rpx;
		padding: 20rpx;
		z-index: 2;

		.list-box {
			margin-top: 58rpx;
		}

		.list {
			background: #ffffff;
			border-radius: 20rpx;
			margin: 30rpx 0;
			padding-top: 30rpx;
			background: linear-gradient(0deg, #EBF5FD, rgba(255, 255, 255, 0.6));
			box-shadow: 0px 0px 18px 0px rgba(23, 70, 128, 0.07);
			border-radius: 10rpx;
			overflow: hidden;

			.list-icon {
				font-size: 68rpx;
			}

			.list-card {
				flex: 1;
				display: flex;
				flex-direction: column;
				// margin-left: 20rpx;
				padding: 0 20rpx 20rpx 20rpx;
				position: relative;

				.list-img {
					position: absolute;
					z-index: -1;
					top: -30rpx;
					left: 0;
					width: 100%;
				}

				.list-card-txt {
					display: flex;
					border-bottom: 2rpx solid #17468012;

					text {
						display: block;
					}

					.title-plan {
						font-size: 34rpx;
						font-weight: 600;
						color: #1E1E20;
					}

					.start-time {
						font-size: 28rpx;
						font-weight: 400;
						color: #979999;
						padding-top: 30rpx;
					}

					.card-fs {
						font-size: 100rpx;
						font-weight: 600;
						text-align: center;
						flex: 1;

						text {
							display: inline-block;
						}
					}
				}
			}

			.list-card-button {
				display: flex;
				justify-content: space-between;
				background: #bfdafb82;
				font-weight: 500;
				color: #1E1E20;
				font-size: 34rpx;
				height: 85rpx;
				line-height: 85rpx;
				align-items: center;
				text-align: center;
				border-radius: 10rpx;
				overflow: hidden;
				color: #000;

				.line {
					width: 3rpx;
					height: 48rpx;
					background: #C5D5EE;
				}
			}
		}
	}

	.subtitle-list {
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
		padding-top: 20rpx;

		.subtitle-list-card {
			flex: 1;
			text-align: center;

			.card-title {
				color: #1E1E20;
				font-weight: 400;
				font-size: 30rpx;
			}

			.card-subtitle {
				font-weight: 500;
				color: #1E1E20;
				font-size: 40rpx;
			}

			text {
				display: block;
			}
		}
	}

	.time-box {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		padding-top: 10rpx;
	}

	.none-data {
		position: absolute;
		top: 600rpx;
		left: 50%;
		transform: translate(-50%, -50%);
		text-align: center;

		.none-img {
			width: 300rpx;
		}

		.none-txt {
			text-align: center;

			text {
				display: block
			}
		}

		.none-btn {
			margin-top: 40rpx;
			border-radius: 8px;
			background-color: rgb(0, 122, 255);
			width: 600rpx;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;

		}
	}
</style>